package com.open.system.dispatch.impl;

import com.open.base.utils.UserHolderUtils;
import com.open.system.dispatch.IUserNoticeDispatch;
import com.open.common.model.Page;
import com.open.system.model.UserNotice;
import com.open.system.service.IUserNoticeService;
import com.open.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户通知关联
 *
 * @author 大树03
 * @date 2025-07-26 19:58
 */
@Component(value="userNoticeDispatch")
@Slf4j
public class UserNoticeDispatchImpl implements IUserNoticeDispatch{

	private IUserNoticeService userNoticeService;

    @Autowired
	public void setUserNoticeService(IUserNoticeService userNoticeService) {
		this.userNoticeService = userNoticeService;
	}

	@Override
	public Boolean save(UserNotice model)  {
		return userNoticeService.save(model);
	}

	@Override
	public Boolean update(UserNotice model)  {
		return userNoticeService.update(model);
	}

	@Override
	public Page<UserNotice> findPage(Page<UserNotice> page,UserNotice item)  {
		return userNoticeService.findPage(page,item);
	}
	
	@Override
	public UserNotice findByPk(Long pk) {
		return userNoticeService.findByPk(pk);
	}

	@Override
    public List<UserNotice> findByModel(UserNotice model)  {
		return userNoticeService.findByModel(model);
	}

	@Override
	public Boolean remove(List<Long> pks) {
		return userNoticeService.remove(pks);
	}

	@Override
	public Boolean removeByUserId(Long userId) {
		return userNoticeService.removeByUserId(userId);
	}

	@Override
	public Boolean batchSave(List<UserNotice> lists) {
		return userNoticeService.batchSave(lists);
	}

	@Override
	public Boolean batchUpdate(List<UserNotice> userNotices) {
		return userNoticeService.batchUpdate(userNotices);
	}

	@Override
	public Boolean isRead(Long id) {
		if (id != null) {
			UserNotice model = new UserNotice();
			model.setRead(1);
			model.setId(id);
			return userNoticeService.update(model);
		}
		UserNotice model = new UserNotice();
		model.setUserId(UserHolderUtils.getUserId());
		List<UserNotice> list = userNoticeService.findByModel(model);
		for (UserNotice notice : list) {
			if (Long.parseLong(notice.getSendTime()) <= DateUtils.getCurrentTimes()) {
				notice.setRead(1);
			}
		}
		return userNoticeService.batchUpdate(list);
	}
}